package com.efun.center.http.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ProjectInvoker {

    private final Logger logger = LoggerFactory.getLogger(getClass());

	protected Class<?> service;

	protected Map<Method, MethodInvoker> methodMap = new ConcurrentHashMap<Method, MethodInvoker>();

	public ProjectInvoker(ProjectLoader projectLoader, Class<?> service) throws Exception {
		this.service = service;

		for (Method method : service.getMethods()) {
			methodMap.put(method, new MethodInvoker(projectLoader, method));
		}
	}

	/**
	 * 调用
	 * 
	 * @param method
	 *            方法
	 * @param params
	 *            参数
	 * @return
	 * @throws Throwable
	 */
	public Object invoke(Method method, Object[] params) throws Throwable {
		logger.info("Begin to invoker service[" + service.getName() + "] method[" + method.getName() + "]");
		return methodMap.get(method).invoke(service, params);
	}

}
